﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using Lucene.Net.Index;
using Lucene.Net.Analysis.Standard;

using CloudSoft.Workflows;

namespace ERPStore.Indexers.Lucene.Workflows.Activities
{

	public sealed class CreateFullTextIndex : CodeActivity
	{
		public InArgument<string> IndexPath { get; set; }
		public InArgument<string> WorkingFolder { get; set; }

		public OutArgument<IndexWriter> IndexWriter { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			string indexPath = context.GetValue(this.IndexPath);
			string workingFolder = context.GetValue(this.WorkingFolder);

			var fullPath = System.IO.Path.Combine(indexPath, workingFolder);
			var directoryInfo = new System.IO.DirectoryInfo(fullPath);
			if (!directoryInfo.Exists)
			{
				System.IO.Directory.CreateDirectory(fullPath);
			}

			var lockFactory = new global::Lucene.Net.Store.NoLockFactory();
			var directory = global::Lucene.Net.Store.FSDirectory.Open(directoryInfo, lockFactory);
			var analyzer = new StandardAnalyzer(global::Lucene.Net.Util.Version.LUCENE_29);
			var indexWriter = new IndexWriter(directory, analyzer , true, global::Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
			// indexWriter.SetUseCompoundFile(true);
			context.SetValue(IndexWriter, indexWriter);
			context.ReportProgress("Repertoire d'indexation pret");
		}
	}
}
